﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using PPI.Client.Logic.Inspection;
using PPI.Entities.Inspections;
using VMind.Global;
using ColumnHeader = DevComponents.AdvTree.ColumnHeader;

namespace PPI.Controls.Grid
{
    public partial class GridInspection : GridBase
    {
        public event EventHandler<GridBaseEventArgs> ClickedAutoDetermine;
        public event EventHandler<GridBaseEventArgs> ClickedEditManual;

        private void OnClickedEditManual(object o, GridBaseEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEditManual != null)
            {
                ClickedEditManual(o, e);
            }
        }

        private List<EInspectionStructure> m_CurrentStructures;

        public List<EInspectionStructure> CurrentStructures
        {
            get { return m_CurrentStructures; }
            set { m_CurrentStructures = value; }
        }

        public List<EInspectionResult> CurrentVisits
        {
            get { return m_CurrentVisits; }
        }

        private List<EInspectionResult> m_CurrentVisits;

        public GridInspection()
        {
            InitializeComponent();
            m_ColumnId = "ID";
        }

        public void LoadVisits(string connectionString)
        {
            m_CurrentVisits = LInspectionResult.GetMobileInspectionResults(connectionString);
            RefreshGrid();
        }

        public void RefreshGrid()
        {
            DataTable dt = new DataTable("Inspection");
            dt.Columns.Add(new DataColumn(m_ColumnId, typeof(Int32)));
            dt.Columns.Add(new DataColumn("IdStructure", typeof(Int32)));
            dt.Columns.Add(new DataColumn("AcquisitionDate", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("Top", typeof(string)));
            dt.Columns.Add(new DataColumn("Company", typeof(string)));
            dt.Columns.Add(new DataColumn("Inspector", typeof(string)));
            dt.Columns.Add(new DataColumn("AssetNumber", typeof(string)));
            dt.Columns.Add(new DataColumn("Latitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Longitude", typeof(double)));

            dt.Columns.Add(new DataColumn("Class", typeof(string)));
            dt.Columns.Add(new DataColumn("Treatment", typeof(string)));
            dt.Columns.Add(new DataColumn("Specie", typeof(string)));
            dt.Columns.Add(new DataColumn("InspectionType", typeof(string)));
            dt.Columns.Add(new DataColumn("InspectionResult", typeof(string)));
            dt.Columns.Add(new DataColumn("Year", typeof(int)));
            dt.Columns.Add(new DataColumn("Length", typeof(int)));
            dt.Columns.Add(new DataColumn("Vendor", typeof(string)));
            dt.Columns.Add(new DataColumn("Project", typeof(string)));
            dt.Columns.Add(new DataColumn("Contact", typeof(string)));

            foreach (EInspectionResult insp in m_CurrentVisits)
            {
                dt.Rows.Add(insp.Id, insp.Inspection.Structure.Id, insp.Inspection.AcquisitionDate, insp.Inspection.TopType.Name, insp.Inspection.Company.Name,
                            insp.Inspection.Inspector.Name, insp.Inspection.Number, insp.Inspection.Latitude, insp.Inspection.Longitude, insp.Inspection.Class.Name,
                            insp.Inspection.Treatment.Name, insp.Inspection.Specie.Name, insp.InspectionType.Name, insp.Resultype.Name,
                            insp.Inspection.Year, insp.Inspection.Length, insp.Inspection.Project.Vendor.Name, insp.Inspection.Project.Name, insp.Inspection.Contact.Name);
            }
            datagridBase.AutoGenerateColumns = true;
            datagridBase.DataSource = null;
            datagridBase.DataSource = dt;
            datagridBase.Columns[0].HeaderText = "Result";
            datagridBase.Columns[1].HeaderText = "Structure";
            datagridBase.Columns[2].HeaderText = "Acquistion";
            datagridBase.Columns[6].HeaderText = "Asset Number";
            datagridBase.Columns[12].HeaderText = "Type";
            datagridBase.Columns[13].HeaderText = "Result";

            datagridBase.Refresh();
           
        }

        protected override void AfterOpenMenu()
        {
            base.AfterOpenMenu();
            cmiAssociateUtility.Visible = false;
            cmiEditStructure.Visible = false;
            cmiEditWhithOutImages.Visible = false;
            //if(getSelectedRow() != null)
            //{
            //    if(ConvertHelper.ToInt32(getSelectedRow().Row["IdStructure"]) > 0)
            //    {
            //        cmiEditStructure.Visible = true;
            //    }else
            //    {
            //        cmiEditStructure.Visible = false;
            //    }
            //}
        }

        private void cmiEditStructure_Click(object sender, EventArgs e)
        {
            if (getSelectedId() != Int32.MinValue)
                OnClickedEditManual(this, new GridBaseEventArgs(getSelectedId(), datagridBase.SelectedRows[0]));

        }
    }
}
